import {formatDate} from '../../../utils/format_utils'
import MyCourse from "./MyCourse";
import {Toast} from 'vant'

let MyCourseMixin = {
  data() {
    return {
      name: '我的课程',
      list: [],
    }
  },
  computed: {
    ///我的课程显示的列表，处理了日期的显示
    courseList() {
      let list = this.list || []
      return list.map((e) => {
        let endDateStr = formatDate(e.endTime)
        if (e.expire) {
          e.expireDateStr = `已过期`
        } else {
          e.expireDateStr = `有效期至${endDateStr}`
        }
        return e
      })
    },
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      Toast.loading({
        duration: 0,
        message: '正在加载',
        forbidClick: true,
        loadingType: "spinner",
      })
      MyCourse.fetchMyCourseListFromWeb()
        .then((courseList) => {
          Toast.clear()
          this.list = courseList
        })
    },
    onItemClick(item) {
      let courseId = item.targetId
      this.$_router.push(`/course/coursedetail/${courseId}`)
    },
  },

}

export default MyCourseMixin
